package wangjg.mda.lang;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author wangjg
 *
 */
class RegExParser {

	private Map<String, Pattern> regexMap = new HashMap<String, Pattern>();
	
	protected Matcher getMatcher(String text, String regex){
		Pattern pattern = regexMap.get(regex);
		if(pattern==null){
			pattern=Pattern.compile(regex, Pattern.MULTILINE|Pattern.DOTALL);
			regexMap.put(regex, pattern);
		}
		Matcher matcher = pattern.matcher(text);
		return matcher;
	}
	
	protected static boolean checkPattern(String regEx, String str) {
		if(str==null)return false;
		java.util.regex.Pattern p = java.util.regex.Pattern.compile(regEx);
		java.util.regex.Matcher m = p.matcher(str);
		return m.matches();
	}

	protected static void _printMatcher(Matcher matcher, int...index){
		if(index.length==0){
			int gCount = matcher.groupCount();
			for(int i=0;i<=gCount;i++){
				System.out.println("group "+i+"\t"+matcher.group(i));
			}
		}else{
			for(int i:index){
				System.out.println("group "+i+"\t"+matcher.group(i));
			}
		}
		System.out.println();
	}
	
}
